<?php

//设置权限
function setGroupHtml($data,$menus,$l = 0){
    foreach ($data as $val) {
        if(!empty($val["subset"])){
            echo '<fieldset class="layui-elem-field"><legend><input type="checkbox" name="menus[]" lay-skin="primary" title="' . $val["menu_name"] . '" value="' . $val["menu_id"] . '" '.(strpos($menus,"[".$val["menu_id"]."]") !== false ? "checked" :"").' ></legend><div class="layui-field-box">';
            foreach ($val["subset"] as $subsetVal) {
                if (!empty($subsetVal['subset'])) {
                    echo '<fieldset class="layui-elem-field"><legend><input type="checkbox" name="menus[]" lay-skin="primary" title="' . $subsetVal["menu_name"] . '" value="' . $subsetVal["menu_id"] . '" '.(strpos($menus,"[".$subsetVal["menu_id"]."]") !== false ? "checked" :"").' ></legend><div class="layui-field-box">';
                    setGroupHtml($subsetVal['subset'], $menus, $l + 1);
                    echo '</div></fieldset>';
                } else {
                    echo '<input type="checkbox" name="menus[]" lay-skin="primary" title="' . $subsetVal["menu_name"] . '" value="' . $subsetVal["menu_id"] .  '" '.(strpos($menus,"[".$subsetVal["menu_id"]."]") !== false ? "checked" :"") .' >';
                }
            }
            echo '</div></fieldset>';
        }else{
            if($l > 0){
                echo '<input type="checkbox" name="menus[]" lay-skin="primary" title="' . $val["menu_name"] . '" value="' . $val["menu_id"] . '" '.(strpos($menus,"[".$val["menu_id"]."]") !== false ? "checked" :"").' >';
            }else{
                echo '<fieldset class="layui-elem-field layui-field-title"><legend><input type="checkbox" name="menus[]" lay-skin="primary" title="' . $val["menu_name"] . '" value="' . $val["menu_id"] . '" '.(strpos($menus,"[".$val["menu_id"]."]") !== false ? "checked" :"").' ></legend></fieldset>';
            }
        }
    }
}

//获取树级菜单
function generateTree($array){
    //第一步 构造数据
    $items = array();
    foreach($array as $value){
        $items[$value['menu_id']] = $value;
    }
    //第二部 遍历数据 生成树状结构
    $tree = array();
    foreach($items as $key => $value){
        if(isset($items[$value['parent_id']])){
            $items[$value['parent_id']]['subset'][] = &$items[$key];
        }else{
            $tree[] = &$items[$key];
        }
    }
    return $tree;
}

//[]字符串分割成数组
function StrToArray($substations){
    if(!empty($substations)) {
        $substations = str_replace("[", '', $substations);//替换所有的[
        $substations = rtrim($substations, "]");//去掉最后一个]
        $substations = explode("]", $substations);//分割成数组
    }
    return !empty($substations) ? $substations : [];
}

//数组组合成字符串
function ArrayToStr($substations){
    $str = "";
    foreach ($substations as $val){
        $str .= "[".$val."]";
    }
    return $str;
}

//还原ID
function SubstationToId($substation_id){
    return rtrim(ltrim($substation_id,"["),"]");
}